home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
cppmatrx.zip
/
MATRIX.ZIP
/
MATRIX.WRK
< prev
next >
Wrap
Text File
|
1991-02-01
|
2KB
|
100 lines
#include <iostream.h>
class matrix {
int *matrix_data;
size_t matrix_size;
public:
matrix (size_t matrix_length);
~matrix (void);
int get_elem (size_t row, size_t column);
int put_elem (size_t row, size_t column, int elem);
int& operator[] (size_t row);
void print_matrix (void);
};
matrix::matrix (size_t matrix_length)
{
matrix_size = matrix_length;
matrix_data = new int [matrix_size];
int *i;
size_t count;
for (i = matrix_data, count = 1; count <= matrix_size*matrix_size; i++, count++)
*i = 0;
cout << "\nCreated object Matrix\n";
}
matrix::~matrix (void)
{
delete matrix_data;
cout << "\nDeleted object Matrix\n";
}
int matrix::get_elem (size_t row, size_t column)
{
return *(matrix_data + (row-1)*matrix_size + (column-1));
}
int matrix::put_elem (size_t row, size_t column, int elem)
{
return (*(matrix_data + (row-1)*matrix_size + (column-1)) = elem);
}
void matrix::print_matrix (void)
{
int *i;
size_t row_count, column_count;
cout << '\n';
for (i = matrix_data, row_count = 0, column_count = 0; row_count < matrix_size; i++) {
cout << *i;
if (column_count == (matrix_size - 1)) {
column_count = 0;
row_count++;
cout << '\n';
}
else
column_count++;
}
}
int& matrix::operator[] (size_t row)
{
// return a reference to the first element in a given row
return *(matrix_data + (row-1)*matrix_size);
}
main (void)
{
matrix matrix_test (8);
int my_elem;
matrix_test.print_matrix ();
matrix_test.put_elem (1,1,8);
my_elem = matrix_test.get_elem (1,1);
cout << '\n' << my_elem << '\n';
cout << "\n-----------------\n";
matrix_test [2] = 16;
matrix_test.print_matrix ();
cout << "\n-----------------\n";
cout << matrix_test [1];
return 0;
}